package com.tianqingwl.gsystem.security.service;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component(value = "gs")
public class GsPermissionConfig {
    public Boolean check(String... permissions){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication==null){
            throw new RuntimeException("登录过期");
        }
        List<String> collect = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        return collect.contains("admin")|| Arrays.stream(permissions).anyMatch(collect::contains);
    }
}
